import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/config/app_config.dart';
import 'package:flutter_douban/config/event_bus_util.dart';
import 'package:flutter_douban/config/sqflite/cook_db_provider.dart';
import 'package:flutter_douban/model/cook_info_model.dart';
import 'package:flutter_douban/tool_views/app_dialog.dart';
import 'package:like_button/like_button.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';

class CollectionImageView extends StatefulWidget {
  final CookInfoModel model;
  const CollectionImageView({Key? key, required this.model}) : super(key: key);

  @override
  _CollectionImageViewState createState() => _CollectionImageViewState();
}

class _CollectionImageViewState extends State<CollectionImageView> {

  Future<bool> onLikeButtonTapped(bool isLiked) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? numberId = prefs.getString(AppConfig.numberId);
    if(numberId == null){
      AppDialog.showText(context, title: "请先登录~");
      return widget.model.is_select!;
    }
    CookDbProvider provider = new CookDbProvider();
    if (widget.model.is_select == true) {
      /// 取消收藏
      provider.delete(widget.model.id!,numberId);
      AppDialog.showText(context, title: "取消收藏成功~");
    } else {
      /// 收藏数据
      provider.insert(widget.model,numberId);
      AppDialog.showText(context, title: "收藏成功~");
    }
    Future.delayed(const Duration(milliseconds: 500), () {
      EventBusUtil.fire(CacheCookEvent(CacheCookEvent.cacheCookIn));
      setState(() {
        widget.model.is_select = !widget.model.is_select!;
      });
    });
    return !widget.model.is_select!;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      margin: EdgeInsets.only(left: 10, right: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [_iconAndcountView(), _toolView()],
      ),
    );
  }

  Widget _iconAndcountView() {
    List<Widget> widgetList = [];
    widget.model.collect_users!.forEach((element) {
      widgetList.add(_avatar(element.p));
    });
    widgetList.add(
      SizedBox(
        width: 10,
      ),
    );
    widgetList.add(
      Text(
        widget.model.collect_count_text!,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 12,
          fontWeight: FontWeight.bold,
          color: AppColors.black,
        ),
      ),
    );
    return Row(
      children: widgetList,
    );
  }

  Widget _toolView() {
    return Row(
      children: [
        LikeButton(
          size: 22,
          isLiked: widget.model.is_select,
          likeBuilder: (bool isLiked) {
            return Image.asset(
                isLiked == true
                    ? 'assets/images/common/collcetionicon_on.png'
                    : "assets/images/common/collcetionicon_off.png",
                color: isLiked == true ? AppColors.yellow : Colors.black87,
                width: 18,
                height: 18);
          },
          onTap: onLikeButtonTapped,
        ),
        IconButton(
            alignment: Alignment.centerRight,
            onPressed: () {},
            icon: Image(
              image: AssetImage("assets/images/common/icon_home_comment.png"),
              fit: BoxFit.cover,
              width: 22,
              height: 22,
            )),
        IconButton(
            alignment: Alignment.centerRight,
            onPressed: () {},
            icon: Image(
              image: AssetImage("assets/images/common/icon_home_share.png"),
              fit: BoxFit.cover,
              width: 22,
              height: 22,
            )),
      ],
    );
  }

  /// 头像
  Widget _avatar(String imageStr) {
    return Row(
      children: [
        Container(
          width: 18,
          height: 18,
          decoration: BoxDecoration(
            color: AppColors.page,
            border: new Border.all(color: Colors.white, width: 1),
            borderRadius: BorderRadius.circular(9),
          ),
          child: ClipOval(
            child: FadeInImage.assetNetwork(
              placeholder: 'assets/images/common/lazy-1.png',
              image: imageStr,
              fit: BoxFit.cover,
            ),
          ),
        ),
      ],
    );
  }
}
